#include <QString>
#include <QSettings>
#include <QtGui>

#include "SettingDialog.h"

SettingDialog::SettingDialog(QSettings *set, QWidget *parent) :
	QDialog(parent), settings(set)
{
	QGridLayout *grid = new QGridLayout(this);
	QButtonGroup *group = new QButtonGroup(this);
	connect(group, SIGNAL(buttonClicked(int)), this, SLOT(chooseDir(int)));
	int row;
	for (row = 0; row != MAX_DEST_COUNT; ++row)
	{
		QLabel *label = new QLabel(QString("%1.").arg(QKeySequence(ShortcutKeys[row]).toString()), this);
		QLineEdit *name = names[row] = new QLineEdit(settings->value(QString("Dest%1/name").arg(row), "Empty").toString(), this);
		QLabel *path = paths[row] = new QLabel(this);
		path->setText(settings->value(QString("Dest%1/path").arg(row), QDir::homePath()).toString());
		QToolButton *chooseDir = new QToolButton(this);
		chooseDir->setText(tr("..."));
		group->addButton(chooseDir, row);
		grid->addWidget(label, row, 0, 1, 1);
		grid->addWidget(name, row, 1, 1, 1);
		grid->addWidget(path, row, 2, 1, 1);
		grid->addWidget(chooseDir, row, 3, 1, 1);
	}

	overwrite = new QCheckBox(tr("Overwrite Existing Files"), this);
	overwrite->setChecked(settings->value("General/overwrite", false).toBool());
	grid->addWidget(overwrite, row++, 0, 1, 4, Qt::AlignLeft);

	copyAsDefault = new QCheckBox(tr("Set Copy as Default"), this);
	copyAsDefault->setChecked(settings->value("General/copy_as_default", false).toBool());
	grid->addWidget(copyAsDefault, row++, 0, 1, 4, Qt::AlignLeft);

	QWidget *buttons = new QWidget(this);
	QHBoxLayout *bl = new QHBoxLayout(buttons);
	QPushButton *ok = new QPushButton(tr("OK"), buttons);
	connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
	QPushButton *cancel = new QPushButton(tr("Cancel"), buttons);
	connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
	bl->addWidget(ok);
	bl->addWidget(cancel);
	grid->addWidget(buttons, row++, 0, 1, 4, Qt::AlignRight);

	resize(600, height());
}

SettingDialog::~SettingDialog()
{}

void SettingDialog::accept()
{
	for (int i = 0; i != MAX_DEST_COUNT; ++i)
	{
		settings->setValue(QString("Dest%1/name").arg(i), names[i]->text());
		settings->setValue(QString("Dest%1/path").arg(i), paths[i]->text());
	}
	settings->setValue(QString("General/overwrite"), overwrite->isChecked());
	settings->setValue(QString("General/copy_as_default"), copyAsDefault->isChecked());
	QDialog::accept();
}

QString SettingDialog::name(int i) const
{
	if (i < 0 || MAX_DEST_COUNT <= i)	return QString();
	return names[i]->text();
}

QString SettingDialog::path(int i) const
{
	if (i < 0 || MAX_DEST_COUNT <= i)	return QString();
	return paths[i]->text();
}

bool SettingDialog::isEnabledOverwrite() const
{
	return overwrite->isChecked();
}

bool SettingDialog::isEnabledCopyAsDefault() const
{
	return copyAsDefault->isChecked();
}

void SettingDialog::chooseDir(int i)
{
	if (i < 0 || MAX_DEST_COUNT <= i)	return;
	QString path = QFileDialog::getExistingDirectory(this, qApp->applicationName());
	if (path.isNull())	return;
	paths[i]->setText(path);
}
